package main

import (
	"fmt"
	"reflect"
	"runtime"
	"strconv"
)

func function(num int) string {
	return strconv.Itoa(num)
}

func functionTwoR(num int) (q, r int) {
	return num / 2, num % 2
}

func eval(a, b int, opt string) (int, error) {
	result := 0
	switch {
	case opt == "+":
		result = a + b
	case opt == "-":
		result = a - b
	case opt == "*":
		result = a * b
	case opt == "/":
		result = a / b
	default:
		return 0, fmt.Errorf("操作符不支持%s", opt)
	}

	return result, nil
}

//交换两个数
func swap(a, b int) (int, int) {
	return b, a
}

//函数式编程
func apply(opt func(int, int) int, a, b int) int {
	pointer := reflect.ValueOf(opt).Pointer()
	name := runtime.FuncForPC(pointer).Name()

	fmt.Printf("调用的方法名为: %s \n", name)
	return opt(a, b)
}

//多参数
func moreParam(nums ...int) {
	for i := range nums {
		fmt.Printf("接收的参数为: %d \n", nums[i])
	}
}

func main() {
	println(function(4))
	q, r := functionTwoR(93)
	fmt.Printf("商为: %d, 余数为: %d \n", q, r)

	if i, err := eval(10, 20, "+"); err != nil {
		fmt.Println("err:", err)
	} else {
		fmt.Println("结果为: ", i)
	}
	a := 4
	b := 5
	i, i2 := swap(a, b)
	fmt.Printf("交换a:%d 和b:%d 之后为: a: %d, b: %d \n", a, b, i, i2)

	i3 := apply(func(i int, i2 int) int {
		return i + i2
	}, a, b)
	fmt.Printf("使用函数式编程之后计算的结果为: %d \n", i3)

	moreParam(1, 2, 3, 4, 5)
}
